import request from "@/router/axios"
import Mock from "mockjs";
const inventorylist = Mock.mock({
    "inventorytableList|50-100":[
        {
            name: "@cword(3, 5)",
            belongsPoints: "@string('lower', 1, 3)",
            "setNumber|10-300":100,
            RequiredSupplier: "@cword(3, 5)",
            "inventoryWarning|100-150":100,
            "id|10-3000":100,
            "status|1": ["已上架","未上架"]
        }
    ]
})
Mock.mock(/\/inventorylistDel/ ,config=>{
    let {id} = JSON.parse(config.body);
    let _list = inventorylist.inventorytableList;
    for(let index in _list){
        if(_list[index].id == id){
            _list.splice(index ,1)
        }
    }
    return{
        data:{
            msg:"ok"
        }
    }
})  
Mock.mock(/\/inventorylistEdit/ ,config=>{
    let {name , setNumber ,id} = JSON.parse(config.body);
    let _list = inventorylist.inventorytableList;
    for(let index in _list){
        if(_list[index].id == id){
            _list[index].name = name;
            _list[index].setNumber = setNumber;
        }
    }
    return{
        data:{
            msg:"ok"
        }
    }
})  
Mock.mock(/\/inventorylist/ ,config=>{
    console.log(config)
    let {pagenum ,pagesize , name} = JSON.parse(config.body);
    // 获取到list的数据里面tableList
    let _list = inventorylist.inventorytableList;
    // 总条数
    let total = _list.length;
    if(name){
        _list = _list.filter((item , index)=>{
            if(item.name.toString().indexOf(name) == -1){
                return false
            }else{
                return true
            }
        })
    }
    // 根据当前第几页 和一页显示几条 返回相应数据
    // user返回的数据
    let user=  _list.filter((item , index) =>index<pagesize*pagenum && index>=pagesize*(pagenum -1));
    // 返回数据
    return{
        data:{
            user , total
        }
    }
    // return {
    //     data:{
    //         list:inventorylist.inventorytableList,
    //         code:200,
    //     }
    // }
})
export const inventoryTableListMock = (data)=>request({
    url:"/inventorylist",
    method:"post",
    data
})

export const inventoryTableListMockDel = (data)=>request({
    url:"/inventorylistDel",
    method:"post",
    data
})
export const inventoryTableListMockEdit = (data)=>request({
    url:"/inventorylistEdit",
    method:"post",
    data
})